package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.JwtUtils;
import company.common.R;
import company.domain.Pets;
import company.domain.Users;
import company.service.UsersService;
import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Resource
    UsersService usersService;

    /*
     * 管理员登录
     * */
    @PostMapping("/LoginUsers")
    public R Login(@RequestBody Users users){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.setEntity(users);
        Users one = this.usersService.getOne(queryWrapper);
        if (one == null){
            return new R().setCode(502).setMessage("用户名密码错误");
        }else{
            HashMap<String,Object> hashMap=new HashMap<>();
            hashMap.put("id",one.getUserid());
            hashMap.put("Username",one.getUsername());
            hashMap.put("Password",one.getPassword());
            String jwtToken= JwtUtils.createJwtToken(hashMap);
            return new R().add("jwtToken",jwtToken);
        }
    }

    /*
     * 图片上传
     * */
    @PostMapping("/uploadfile")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\HTML\\cat-html\\CAY\\img"+file.getOriginalFilename()));
        return new R().add("fname",file.getOriginalFilename());
    }

    @PostMapping("/uploadfile2")
    public R uploadFile2(@RequestParam("file2") MultipartFile file2) throws IOException {
        System.out.println(file2.getOriginalFilename());
        file2.transferTo(new File("D:\\HTML\\cat-html\\CAY\\img"+file2.getOriginalFilename()));
        return new R().add("fname2",file2.getOriginalFilename());
    }

    /*
    * 用户修改
    * */
    @PostMapping("/uploadUser")
    public R uploadUser(@RequestBody Users users){
        this.usersService.updateById(users);
        return new R();
    }

    /*
    * 注册用户
    * */
    @PostMapping("/AddUser")
    public R AddUser(@RequestBody Users users){
        this.usersService.save(users);
        return new R();
    }

    /*
    * 用户查询
    * */
    @RequestMapping("/selectAllPage/{page}")
    public R selectAllPage(@PathVariable("page") int page,@RequestBody Users users){
        QueryWrapper<Users> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(users.getUsername()!="" && !("".equals(users.getUsername())),"username",users.getUsername());
        queryWrapper.like(users.getEmail()!="" && !("".equals(users.getEmail())),"email",users.getEmail());
        queryWrapper.like(users.getAddress()!="" && !("".equals(users.getAddress())),"address",users.getAddress());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,4);
        List<Users> list=this.usersService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }
    @RequestMapping("/selectAll2")
    public R selectAll2(){

        List<Users> list=this.usersService.list();
        return new R().add("list",list);
    }
    /*
    * 用户删除
    * */
    @RequestMapping("/DelUser/{UserID}")
    public R DelUser(@PathVariable("UserID") int UserID){
        boolean remove = this.usersService.removeById(UserID);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据用户id查询
     * */
    @PostMapping("/selectUserbyid")
    public R selectUserbyid(@RequestBody Users users){
        Users byId = this.usersService.getById(users.getUserid());
        return new R().add("byId",byId);
    }
}
